package com.windoent.yugioh.adapter.brvah;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.windoent.yugioh.R;
import com.windoent.yugioh.bean.SaiShiListDataBean;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.SimpleUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author : robert
 * date   : 2021/5/6 15:26
 * desc   :
 */
public class SaiShiHomePageAdapter extends BaseQuickAdapter<SaiShiListDataBean.DataBean.MatchsBean, BaseViewHolder> {

    private List<TextView> tvLists = new ArrayList<>();

    public SaiShiHomePageAdapter() {
        super(R.layout.item_saishi_home_page_sign_uping_xunhui, null);
    }

    //显示预约字段
    private void showYuYueText(boolean show, TextView tvStatusLeft, TextView tvStatusRight, TextView tvStatusLeftKuoHao) {
        tvStatusLeft.setVisibility(show ? View.VISIBLE : View.GONE);
        tvStatusRight.setVisibility(show ? View.VISIBLE : View.GONE);
        tvStatusLeftKuoHao.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SaiShiListDataBean.DataBean.MatchsBean item) {
        helper.setText(R.id.tv_game_type_name, getTypeName(item.getType()))
                .setText(R.id.tv_game_name, BaseMethod.notNullDefaultValueNull(item.getName()))
                .setText(R.id.tv_location_name, BaseMethod.notNullDefaultValueNull(item.getAddress()))
                .setText(R.id.tv_time_name, BaseMethod.notNullDefaultValueNull(item.getStartAt()))
        ;
        ImageView imageView = helper.getView(R.id.image_type);
        ImageView imageViewIcon = helper.getView(R.id.iv_game_type_icon);
        TextView tvStatus = helper.getView(R.id.tv_status);
        TextView tvStatusLeft = helper.getView(R.id.tv_status_left);
        TextView tvStatusRight = helper.getView(R.id.tv_status_riht);
        TextView tvStatusLeftKuoHao = helper.getView(R.id.tv_status_left_kuohao);
        RelativeLayout mRlYueYueSucess = helper.getView(R.id.rl_yuyue);//预约成功
        int type = item.getType();
        switch (type) {
            //1:娱乐赛2：积分赛3：特殊赛4：巡回赛
            case 4:
                //巡回
                imageView.setBackgroundResource(R.drawable.drawable_xunhui_games_bg);
                imageViewIcon.setBackgroundResource(R.drawable.drawable_xunhui_games_icon);
                break;
            case 2:
                //积分
                imageView.setBackgroundResource(R.drawable.drawable_jifen_games_bg);
                imageViewIcon.setBackgroundResource(R.drawable.drawable_jifen_games_icon);
                break;
            case 3:
                //特殊
                imageView.setBackgroundResource(R.drawable.drawable_teshu_games_bg);
                imageViewIcon.setBackgroundResource(R.drawable.drawable_teshu_games_icon);
                break;
            case 1:
                //娱乐
                imageView.setBackgroundResource(R.drawable.drawable_yule_games_bg);
                imageViewIcon.setBackgroundResource(R.drawable.drawable_yule_games_icon);
                break;
        }
        SaiShiListDataBean.DataBean.MatchsBean.BottomBean bottom = item.getBottom();
        mRlYueYueSucess.setVisibility(bottom != null && bottom.getTitle() != null && bottom.getTitle().isReserve() ? View.VISIBLE : View.GONE);
        if (bottom != null) {
            int typeBottom = bottom.getType();//1：纯文字样式 2：带有报名人数样式 3：倒计时样式
            SaiShiListDataBean.DataBean.MatchsBean.BottomBean.TitleBean title = bottom.getTitle();
            if (typeBottom == 1) {
                if (title != null) {
                    showYuYueText(false, tvStatusLeft, tvStatusRight, tvStatusLeftKuoHao);
                    tvStatus.setText(title.getText());
                }
            } else if (typeBottom == 2) {
                if (title != null) {
                    showYuYueText(false, tvStatusLeft, tvStatusRight, tvStatusLeftKuoHao);
                    String text = title.getText();
                    int alreadySignedUp = title.getAlreadySignedUp();
                    int signUpTotal = title.getSignUpTotal();
                    SpannableStringBuilder textSpannedBuilder = new SpannableStringBuilder();
                    SpannableString textSpanned1 = new SpannableString(text + "：");
                    textSpanned1.setSpan(new StyleSpan(Typeface.BOLD),
                            0, textSpanned1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textSpanned1.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFFFF")), 0, textSpanned1.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    SpannableString textSpanned2 = new SpannableString(alreadySignedUp + "");
                    textSpanned2.setSpan(new StyleSpan(Typeface.BOLD),
                            0, textSpanned2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textSpanned2.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFF4C4B")), 0, textSpanned2.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    SpannableString textSpanned3 = new SpannableString("/" + signUpTotal);
                    textSpanned3.setSpan(new StyleSpan(Typeface.BOLD),
                            0, textSpanned3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textSpanned3.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFFFF")), 0, textSpanned3.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    tvStatus.setText(textSpannedBuilder.append(textSpanned1).append(textSpanned2).append(textSpanned3));
                }
            } else if (typeBottom == 3) {
                if (title != null && title.isFirst()) {
                    showYuYueText(BaseMethod.notNull(title.getText()), tvStatusLeft, tvStatusRight, tvStatusLeftKuoHao);
                    tvStatusLeft.setText(title.getText());
                    if (title.getCountdown() > 0) {
                        if (!tvLists.contains(tvStatus)) {
                            tvLists.add(tvStatus);
                        }
                        tvStatus.setText(SimpleUtils.time2String(title.getCountdown()));
                    } else {
                        tvStatus.setText("00:00:00");
                    }
                    title.setFirst(false);
                }
            }
        }
    }

    private String getTypeName(int type) {
        //1:娱乐赛2：积分赛3：特殊赛4：巡回赛
        if (type == 1) {
            return getContext().getString(R.string.string_entertainment_race);
        }
        if (type == 2) {
            return getContext().getString(R.string.string_point_race);
        }
        if (type == 3) {
            return getContext().getString(R.string.string_teshu_race);
        }
        if (type == 4) {
            return getContext().getString(R.string.string_xunhui_race);
        }
        return "";
    }

    public void resetNotifyTime() {
        tvLists.clear();
    }

    public void notifyTime() {
        if (tvLists != null && tvLists.size() > 0) {
            for (int i = 0; i < tvLists.size(); i++) {
                String string = tvLists.get(i).getText().toString();
                int string2Time = SimpleUtils.string2Time(string);
                if (string2Time <= 0) {
                    tvLists.get(i).setText("00:00:00");
                } else {
                    tvLists.get(i).setText(SimpleUtils.time2String(string2Time));
                }
            }
        }
    }
}